/**
 * @(#)RetentFilterConfigServiceImpl.java 2017年7月4日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsbd.boss.dao.IActionTypeDAO;
import org.jsbd.boss.dao.IRetentFilterConfigDao;
import org.jsbd.boss.domian.channel.ActionType;
import org.jsbd.boss.domian.channel.RetentFilterConfig;
import org.jsbd.boss.service.IRetentFilterConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * TODO 请加入本类的说明
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class RetentFilterConfigServiceImpl implements IRetentFilterConfigService {
	public static final Logger LOGGER = LoggerFactory.getLogger(RetentFilterConfigServiceImpl.class);

	@Resource
	private IRetentFilterConfigDao retentFilterConfigDao;
	@Resource
	private IActionTypeDAO actionTypeDao;

	private boolean isExists(Integer loginType, List<RetentFilterConfig> list) {
		for (RetentFilterConfig filter : list) {
			if (filter.getActionType().intValue() == loginType.intValue()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<RetentFilterConfig> selectRetentFilterConfigList(int apkId) {
		RetentFilterConfig filter = new RetentFilterConfig();
		filter.setApkId(apkId);
		List<RetentFilterConfig> targetList = new ArrayList<RetentFilterConfig>();
		List<RetentFilterConfig> list = retentFilterConfigDao.selectRetentFilterConfigList(filter);
		targetList.addAll(list);
		List<ActionType> actionTypeList = actionTypeDao.selectAll();
		for (ActionType actionType : actionTypeList) {
			RetentFilterConfig config = new RetentFilterConfig();
			config.setActionType(actionType.getCode());
			config.setApkId(apkId);
			config.setName(actionType.getName());
			if (list != null && list.size() > 0) {
				if (!isExists(actionType.getCode(), list)) {
					config.setEnable(0);
					config.setPriority(0);
					targetList.add(config);
				}
			} else {
				config.setEnable(0);
				config.setPriority(0);
				targetList.add(config);
			}
		}
		return targetList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.IRetentFilterConfigService#
	 * insertAndUpdateRetentFilterConfig
	 * (org.jsbd.boss.domian.channel.RetentFilterConfig)
	 */
	@Override
	public void insertAndUpdateRetentFilterConfig(RetentFilterConfig config) {
		retentFilterConfigDao.insertRetentFilterConfig(config);
	}

	@Override
	public List<RetentFilterConfig> selectALLRetentFilterConfigList() {
		List<RetentFilterConfig> targetList = new ArrayList<RetentFilterConfig>();
		List<ActionType> actionTypeList = actionTypeDao.selectAll();
		for (ActionType actionType : actionTypeList) {
			RetentFilterConfig config = new RetentFilterConfig();
			config.setActionType(actionType.getCode());
			config.setName(actionType.getName());
			config.setEnable(0);
			config.setPriority(0);
			targetList.add(config);
		}
		return targetList;
	}

	@Override
	public void batchInsertAndUpdateRetentFilterConfig(List<RetentFilterConfig> list) {
		retentFilterConfigDao.batchInsertRetentFilterConfig(list);
	}

}
